package com.xmtlxx.web.controller.tms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.xmtlxx.web.controller.tms.service.IOrderDtlService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.tms.domain.OrderDtl;
import com.xmtlxx.tms.domain.bo.OrderDtlBo;
import com.xmtlxx.tms.domain.vo.OrderDtlVo;
import com.xmtlxx.tms.mapper.OrderDtlMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 订单明细Service业务层处理
 *
 * @author xmtlxx
 * @date 2022-08-15
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class OrderDtlServiceImpl implements IOrderDtlService {

    private final OrderDtlMapper baseMapper;

    /**
     * 查询订单明细
     *
     * @param id 订单明细主键
     * @return 订单明细
     */
    @Override
    public OrderDtlVo queryById(String id){
        //return baseMapper.selectVoById(id);
        return baseMapper.selectOrderDtlVoByID(id);
    }

    /**
     * 查询订单明细列表
     *
     * @param bo 订单明细
     * @return 订单明细
     */
    @Override
    public TableDataInfo<OrderDtlVo> queryPageList(OrderDtlBo bo, PageQuery pageQuery) {
       // LambdaQueryWrapper<OrderDtl> lqw = buildQueryWrapper(bo);
       // Page<OrderDtlVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<OrderDtlVo> result = baseMapper.selectPageOrderDtlList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询订单明细列表
     *
     * @param bo 订单明细
     * @return 订单明细
     */
    @Override
    public List<OrderDtlVo> queryList(OrderDtlBo bo) {
       // LambdaQueryWrapper<OrderDtl> lqw = buildQueryWrapper(bo);
       // return baseMapper.selectVoList(lqw);
        return baseMapper.selectOrderDtlList(bo);
    }

    private LambdaQueryWrapper<OrderDtl> buildQueryWrapper(OrderDtlBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OrderDtl> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getOrderId()), OrderDtl::getOrderId, bo.getOrderId());
        lqw.eq(StringUtils.isNotBlank(bo.getPno()), OrderDtl::getPno, bo.getPno());
        lqw.like(StringUtils.isNotBlank(bo.getName()), OrderDtl::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), OrderDtl::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), OrderDtl::getDelFlag, bo.getDelFlag());
        return lqw;
    }

    /**
     * 新增订单明细
     *
     * @param bo 订单明细
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(OrderDtlBo bo) {
        OrderDtl add = BeanUtil.toBean(bo, OrderDtl.class);
        validEntityBeforeSave(add);

        //填充UUID
        add.setId(IdUtil.simpleUUID());
        add.setDelFlag("0");
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改订单明细
     *
     * @param bo 订单明细
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(OrderDtlBo bo) {
        OrderDtl update = BeanUtil.toBean(bo, OrderDtl.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(OrderDtl entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除订单明细
     *
     * @param ids 需要删除的订单明细主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
